<template>
  <div class="echarts-demo">
    <h1>ECharts 示例</h1>
    <div ref="chart" style="width: 600px; height: 400px; margin: 0 auto;"></div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import * as echarts from 'echarts'

export default defineComponent({
  setup() {
    const chart = ref<HTMLElement | null>(null)

    onMounted(() => {
      if (chart.value) {
        const myChart = echarts.init(chart.value)

        const option = {
          title: {
            text: '销售数据统计',
            left: 'center'
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: ['电子产品', '服装', '食品'],
            bottom: 10
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '15%',
            containLabel: true
          },
          xAxis: {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月']
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: '电子产品',
              type: 'line',
              data: [120, 132, 101, 134, 90, 230, 210],
              smooth: true,
              lineStyle: {
                width: 3
              }
            },
            {
              name: '服装',
              type: 'bar',
              data: [150, 232, 201, 154, 190, 330, 410],
              itemStyle: {
                color: '#36a3ef'
              }
            },
            {
              name: '食品',
              type: 'line',
              data: [320, 332, 301, 334, 390, 330, 320],
              smooth: true,
              lineStyle: {
                width: 3,
                type: 'dashed'
              }
            }
          ]
        }

        myChart.setOption(option)

        // 响应式调整
        window.addEventListener('resize', () => {
          myChart.resize()
        })
      }
    })

    return {
      chart
    }
  }
})
</script>

<style scoped>
.echarts-demo {
  text-align: center;
  padding: 20px;
}
</style>